package com.example.linktoplinktop.phoneexcele;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    private EditText editView;
    private String number;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* 加载布局 */
        setContentView(R.layout.activity_main);



        /*获取控件*/

        editView = (EditText) findViewById(R.id.editView);
        Button btn = (Button) findViewById(R.id.btn);

        //点击事件
        btn.setOnClickListener(new PhoneClickListener());



        //匿名内部类实现点击方法
        Button btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("匿名内部类");

            }
        });




    }

    /** 实现点击事件回调接口 */
    private class PhoneClickListener implements View.OnClickListener {


        @Override
        public void onClick(View view) {
            clikMethod();


        }
    }

    private void clikMethod() {
        //取到edittext文本
        String phoneNumber = editView.getText().toString().trim(); //trim() 去掉空格
        System.out.println("按钮被点击" + phoneNumber);
        //判空提示
        if ("".equals(phoneNumber)) {

            Toast.makeText(MainActivity.this,"号码不能为空", Toast.LENGTH_LONG).show();

            return;
        }

        number = phoneNumber;

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) !=
                PackageManager.PERMISSION_GRANTED) {
            //动态权限
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);


        } else {

            myCall(number);

        }
    }


    /**
     * 权限回调
     *
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == 1) {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                myCall(number);

            } else  {

                Toast.makeText(MainActivity.this, "拒接打电话", Toast.LENGTH_SHORT);

            }


        }


        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    public void myCall(String number) {
        //打电话
        Intent callIntent = new Intent();
        //设置动作
        callIntent.setAction(Intent.ACTION_CALL);
        //设置数据
        callIntent.setData(Uri.parse("tel:"+number));
        //开启意图
        startActivity(callIntent);

    }

    //第四种 xml中属性设置方法名  注意: 方法必须是public, 不然找不到方法

    public void selfThirdClick(View v) {


        System.out.println("第四种点击xml");

    }


}
